{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "princess_count = 3\n",
    "\n",
    "# 顺手就用整数了\n",
    "w1 = np.array([100,99,97])\n",
    "w2 = np.array([5,4,1])\n",
    "w3 = np.array([10,7,3])\n",
    "\n",
    "#                   公主1         公主2      公主3\n",
    "# 王子1 彩礼        100           99         97\n",
    "# 王子2 彩礼         5            4          1\n",
    "# 王子3 彩礼        10            7          3\n",
    "\n",
    "prices = np.vstack([w1,w2,w3])\n",
    "print(np.ndim(prices))\n",
    "\n",
    "n_a = np.linspace(1,100,25)\n",
    "prices = n_a.reshape(5,5) # 也可用-1根据剩下的数计算，只要是方阵就好\n",
    "\n",
    "def cal(x,y):\n",
    "    return (x+1)*(y+1)\n",
    "\n",
    "prices = np.fromfunction(cal,(9,9))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "achieve = 0\n",
    "# 循环 princess_count 次，取出 princess_count 个数相加\n",
    "\n",
    "# 假设同一个王子不会给多个公主相同的彩礼\n",
    "def get_largest(prices):\n",
    "    row_index = -1\n",
    "    col_index = -1\n",
    "    l_range = -1\n",
    "    result = 0,row_index,col_index\n",
    "    \n",
    "    for one in prices:\n",
    "        print(one)\n",
    "        row_index += 1\n",
    "        col_index = np.argmax(one)\n",
    "        o_range = np.ptp(one) # 求极差\n",
    "        if(l_range < o_range):\n",
    "            l_range = o_range\n",
    "            result = one[col_index],row_index,col_index\n",
    "    # print(result)\n",
    "    return result\n",
    "\n",
    "def get_range(prices, achieve):\n",
    "    largest,row_index,col_index = get_largest(prices)\n",
    "    achieve += largest\n",
    "    print(achieve)\n",
    "    cofactor = np.delete(prices,row_index,axis = 0)\n",
    "    cofactor = np.delete(cofactor,col_index,axis = 1)\n",
    "    print(cofactor)\n",
    "    print(np.size(cofactor))\n",
    "    \n",
    "    if(np.size(cofactor) == 0):\n",
    "        return achieve\n",
    "    else:\n",
    "        return get_range(cofactor, achieve)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1. 2. 3. 4. 5. 6. 7. 8. 9.]\n",
      "[ 2.  4.  6.  8. 10. 12. 14. 16. 18.]\n",
      "[ 3.  6.  9. 12. 15. 18. 21. 24. 27.]\n",
      "[ 4.  8. 12. 16. 20. 24. 28. 32. 36.]\n",
      "[ 5. 10. 15. 20. 25. 30. 35. 40. 45.]\n",
      "[ 6. 12. 18. 24. 30. 36. 42. 48. 54.]\n",
      "[ 7. 14. 21. 28. 35. 42. 49. 56. 63.]\n",
      "[ 8. 16. 24. 32. 40. 48. 56. 64. 72.]\n",
      "[ 9. 18. 27. 36. 45. 54. 63. 72. 81.]\n",
      "81.0\n",
      "[[ 1.  2.  3.  4.  5.  6.  7.  8.]\n",
      " [ 2.  4.  6.  8. 10. 12. 14. 16.]\n",
      " [ 3.  6.  9. 12. 15. 18. 21. 24.]\n",
      " [ 4.  8. 12. 16. 20. 24. 28. 32.]\n",
      " [ 5. 10. 15. 20. 25. 30. 35. 40.]\n",
      " [ 6. 12. 18. 24. 30. 36. 42. 48.]\n",
      " [ 7. 14. 21. 28. 35. 42. 49. 56.]\n",
      " [ 8. 16. 24. 32. 40. 48. 56. 64.]]\n",
      "64\n",
      "[1. 2. 3. 4. 5. 6. 7. 8.]\n",
      "[ 2.  4.  6.  8. 10. 12. 14. 16.]\n",
      "[ 3.  6.  9. 12. 15. 18. 21. 24.]\n",
      "[ 4.  8. 12. 16. 20. 24. 28. 32.]\n",
      "[ 5. 10. 15. 20. 25. 30. 35. 40.]\n",
      "[ 6. 12. 18. 24. 30. 36. 42. 48.]\n",
      "[ 7. 14. 21. 28. 35. 42. 49. 56.]\n",
      "[ 8. 16. 24. 32. 40. 48. 56. 64.]\n",
      "145.0\n",
      "[[ 1.  2.  3.  4.  5.  6.  7.]\n",
      " [ 2.  4.  6.  8. 10. 12. 14.]\n",
      " [ 3.  6.  9. 12. 15. 18. 21.]\n",
      " [ 4.  8. 12. 16. 20. 24. 28.]\n",
      " [ 5. 10. 15. 20. 25. 30. 35.]\n",
      " [ 6. 12. 18. 24. 30. 36. 42.]\n",
      " [ 7. 14. 21. 28. 35. 42. 49.]]\n",
      "49\n",
      "[1. 2. 3. 4. 5. 6. 7.]\n",
      "[ 2.  4.  6.  8. 10. 12. 14.]\n",
      "[ 3.  6.  9. 12. 15. 18. 21.]\n",
      "[ 4.  8. 12. 16. 20. 24. 28.]\n",
      "[ 5. 10. 15. 20. 25. 30. 35.]\n",
      "[ 6. 12. 18. 24. 30. 36. 42.]\n",
      "[ 7. 14. 21. 28. 35. 42. 49.]\n",
      "194.0\n",
      "[[ 1.  2.  3.  4.  5.  6.]\n",
      " [ 2.  4.  6.  8. 10. 12.]\n",
      " [ 3.  6.  9. 12. 15. 18.]\n",
      " [ 4.  8. 12. 16. 20. 24.]\n",
      " [ 5. 10. 15. 20. 25. 30.]\n",
      " [ 6. 12. 18. 24. 30. 36.]]\n",
      "36\n",
      "[1. 2. 3. 4. 5. 6.]\n",
      "[ 2.  4.  6.  8. 10. 12.]\n",
      "[ 3.  6.  9. 12. 15. 18.]\n",
      "[ 4.  8. 12. 16. 20. 24.]\n",
      "[ 5. 10. 15. 20. 25. 30.]\n",
      "[ 6. 12. 18. 24. 30. 36.]\n",
      "230.0\n",
      "[[ 1.  2.  3.  4.  5.]\n",
      " [ 2.  4.  6.  8. 10.]\n",
      " [ 3.  6.  9. 12. 15.]\n",
      " [ 4.  8. 12. 16. 20.]\n",
      " [ 5. 10. 15. 20. 25.]]\n",
      "25\n",
      "[1. 2. 3. 4. 5.]\n",
      "[ 2.  4.  6.  8. 10.]\n",
      "[ 3.  6.  9. 12. 15.]\n",
      "[ 4.  8. 12. 16. 20.]\n",
      "[ 5. 10. 15. 20. 25.]\n",
      "255.0\n",
      "[[ 1.  2.  3.  4.]\n",
      " [ 2.  4.  6.  8.]\n",
      " [ 3.  6.  9. 12.]\n",
      " [ 4.  8. 12. 16.]]\n",
      "16\n",
      "[1. 2. 3. 4.]\n",
      "[2. 4. 6. 8.]\n",
      "[ 3.  6.  9. 12.]\n",
      "[ 4.  8. 12. 16.]\n",
      "271.0\n",
      "[[1. 2. 3.]\n",
      " [2. 4. 6.]\n",
      " [3. 6. 9.]]\n",
      "9\n",
      "[1. 2. 3.]\n",
      "[2. 4. 6.]\n",
      "[3. 6. 9.]\n",
      "280.0\n",
      "[[1. 2.]\n",
      " [2. 4.]]\n",
      "4\n",
      "[1. 2.]\n",
      "[2. 4.]\n",
      "284.0\n",
      "[[1.]]\n",
      "1\n",
      "[1.]\n",
      "285.0\n",
      "[]\n",
      "0\n",
      "285.0\n"
     ]
    }
   ],
   "source": [
    "achieve = get_range(prices, achieve)\n",
    "print(achieve)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
